package net.zabuchy.user.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;

import net.zabuchy.address.entity.Address;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.project.entity.Project;

@Entity
@Table(name="USER", catalog="quickstart")
public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;

	private int userId;
	private int customerId;
	private String username;
	private String password;
	private boolean enabled;
	private String firstName;
	private String secondName;
	private String email;
	private String fax;
	private String phone;
	private Address address;
	private Date creationDate;
	private Date lastVisit;
	private Set<Project> projects = new HashSet<Project>(0);
	private Set<Meeting> meetings = new HashSet<Meeting>(0);
	private Set<Issue> issues = new HashSet<Issue>(0);


	public User() {
    }
    
    public User(int customerId, String username, String password,
			boolean enabled, String firstName, String secondName, String email,
			String fax, String phone, Address address, Date creationDate,
			Date lastVisit) {
		super();
		this.customerId = customerId;
		this.username = username;
		this.password = password;
		this.enabled = enabled;
		this.firstName = firstName;
		this.secondName = secondName;
		this.email = email;
		this.fax = fax;
		this.phone = phone;
		this.address = address;
		this.creationDate = creationDate;
		this.lastVisit = lastVisit;
	}

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="USER_ID", unique=true, nullable=false)
	public int getUserId() {
		return this.userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	@Column(name="CUSTOMER_ID", nullable=false)
	public int getCustomerId() {
		return this.customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	@Column(name="USERNAME", unique=true, nullable=false)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name="PASSWORD", nullable=false)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name="ENABLED", nullable=false)
	public boolean getEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	

	@Column(name="FIRST_NAME")
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name="SECOND_NAME")
	public String getSecondName() {
		return secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	@Column(name="EMAIL")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name="FAX")
	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	@Column(name="PHONE")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Temporal( TemporalType.TIMESTAMP)
	@Column(name="CREATION_DATE")
	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	@Temporal( TemporalType.TIMESTAMP)
	@Column(name="LAST_VISIT")
	public Date getLastVisit() {
		return lastVisit;
	}

	public void setLastVisit(Date lastVisit) {
		this.lastVisit = lastVisit;
	}

	@ManyToOne(fetch=FetchType.EAGER)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@JoinColumn(name="ADDRESS_ID")
	public Address getAddress() {
		return this.address;
	}
	/*@OneToOne(cascade = CascadeType.ALL)
	public Address getAddress() {
		return address;
	}*/
	
	public void setAddress(Address address) {
		this.address = address;
	}

	@ManyToMany(fetch=FetchType.EAGER)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@JoinTable(name="USER_PROJECTS", catalog="quickstart", joinColumns={
		@JoinColumn(name="USER_ID", nullable=false, updatable=false)},
		inverseJoinColumns = { @JoinColumn(name="PROJECT_ID", nullable=false, updatable=false)})
	public Set<Project> getProjects() {
		return projects;
	}

	public void setProjects(Set<Project> projects) {
		this.projects = projects;
	}
	
	@ManyToMany(fetch=FetchType.EAGER)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@JoinTable(name="USER_MEETINGS", catalog="quickstart", joinColumns={
		@JoinColumn(name="USER_ID", nullable=false, updatable=false)},
		inverseJoinColumns = { @JoinColumn(name="MEETING_ID", nullable=false, updatable=false)})
	public Set<Meeting> getMeetings() {
		return meetings;
	}

	public void setMeetings(Set<Meeting> meetings) {
		this.meetings = meetings;
	}

	@ManyToMany(fetch=FetchType.EAGER)
	@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	@JoinTable(name="ASSIGMENTS", catalog="quickstart", joinColumns={
		@JoinColumn(name="USER_ID", nullable=false, updatable=false)},
		inverseJoinColumns = { @JoinColumn(name="ISSUE_ID", nullable=false, updatable=false)})
	public Set<Issue> getIssues() {
		return issues;
	}

	public void setIssues(Set<Issue> issues) {
		this.issues = issues;
	}

}